from flask_login import login_required, login_user, logout_user
from blog.model.User import  User
from blog.model.Category import  Category
import os
from blog import app
from flask import request,render_template,flash,abort,url_for,redirect,session,Flask,g

@app.route('/')
@login_required
def show_entries():
    categorys = Category.query.all()
    return render_template('show_entries.html',entries=categorys)


@app.route('/indexshow')
def indexshow():
    return render_template('indexshow.html')


@app.route('/login',methods=['GET','POST'])
def login():
    error = None
    if request.method == 'POST':
        print(" if request.method == 'POST':")
        # id = User.query.filter_by(username=request.form['username']).first()
        # print(id)
        user = User.query.filter_by(username=request.form['username']).first()
        passwd = User.query.filter_by(password=request.form['password']).first()
        if user is None:
           print("error = 'Invalid username'")
        elif passwd is None:
            print("error = 'Invalid password'")
        else:
            session['logged_in'] = True
            flash('You were logged in')
            print("执行login前")
            #重写is_active方法
            login_user(user)
            print("执行login后")
            # 消息闪现
            flash('Logged in successfully.')
            return redirect(url_for('login'))
    return render_template('login.html')

@app.route('/logout')
@login_required
def logout():
    session.pop('logged_in', None)
    logout_user()
    flash('You were logged out')
    return redirect(url_for('indexshow'))






# def Response_headers(content):
#     resp = Response(content)
#     resp.headers['Access-Control-Allow-Origin'] = '*'
#     return resp

#获取登录参数及处理
# @app.route('/login')
# def getLoginRequest():
#     #查询用户名及密码是否匹配及存在
#     #连接数据库,此前在数据库中创建数据库TESTDB
#
